import Vue 				from 'vue'
import util 			from '../../util'
import axios 			from 'axios'
import { List,Toast,Picker} 	from 'vant'
Vue.use(List,Toast,Picker)

export default {
	name: 'backList',
	data(){
		return {
			pageState:1,		//1:下线管理   2:邀请记录
			pageIndex:1,		//页数
			list:[],
			loading:false,
			finished:false,
			loadError:false,
			offset:200,
			// token:'',
			user:0,				//0待定   1普通用户
			currentId:'USDT',		//当前币种
			rate:{},
			shown:false,
			rebateE2:'',
			contractAmountE2:'',
			isSuperPartner:'0',		//是否超级合伙人 1:是  0:否
			summary:{},
		}
	},
	computed:{
		// lang(){
		// 	return this.$i18n.locale
		// },
		columns(){
			let _arr = [this.$t('backList-key10'),this.$t('backList-key11'),this.$t('backList-key12')];
			let _nowDate = new Date();
			let _y = _nowDate.getFullYear();
			let _m = _nowDate.getMonth()+1;
			if(_m && _m.toString().length == 1){
				_m = '0'+_m;
			}
			_arr.push(_y+'-'+_m)
			return _arr
		},
		// isIphoneX(){
		// 	return util.isIphoneX()
		// },
		// isIphone(){
		// 	return util.client()
		// }
	},
	methods:{
		init(){
			let _last = this.columns[this.columns.length-1];
			let _arr = _last.split('-');
			if(_arr[0] == '2019'){
				this.columns.push('2019-11','2019-10','2019-09')
			}else{
				let _y = _arr[0];
				let _m = _arr[1];
				if(Number(_m) == 1){
					_y = _y-1;
					_m = 12;
				}else{
					_m = Number(_m) - 1;
				}
				if(_m && _m.toString().length == 1){
					_m = '0'+_m;
				}
				this.columns.push(_y+'-'+_m);
				this.init()
			}
		},
		onConfirm(e){
			//点击确定
			this.pageIndex = 1;
			this.list = [];
			this.finished = false;
			this.loading = true;
			this.defaultDay = this.current.value;
			this.ctrlShown();
			this.load();
		},
		onChange(picker, value, index) {
			//拖动选择停止的时候保存数据
			this.current = {
				value:value,
				index:index
			}
    	},
		load(){
			this.loading = true;
			let self = this;
			setTimeout(()=>{
				let params = {
					token:self.token || window.token || util.getToken(),
					page:self.pageIndex+'',
					size:'20'
				}
				if(self.current.index == 0){
					//选择全部
				}else if(self.current.index == 1){
					//选择今日
					let _timeArr = util.transTime4(+new Date());
					params.day = _timeArr[0]+_timeArr[1]+_timeArr[2];
				}else if(self.current.index == 2){
					//选择昨日
					let _timeArr = util.transTime4(+new Date() - 24*60*60*1000);
					params.day = _timeArr[0]+_timeArr[1]+_timeArr[2];
				}else{
					//直接选择月份
					params.month = self.current.value.replace('-','');
				}
				util.fetch(util.ajaxUrl+'/commission/summary',params).then(res => {
					self.loading = false;
					if(res.retCode === 0){
						self.rate = res.retData.rate;
						self.pageIndex++;
						self.rebateE2 = res.retData.rebate_e2;
						self.contractAmountE2 = res.retData.contract_amount_e2;
						self.summary = res.retData.summary;
						if(res.retData.lists.length){
							res.retData.lists.map(item => {
								item.total = util.accAdd(Number(item.indirect),Number(item.direct))
							})
						}
						if(res && res.retData && res.retData.lists && res.retData.lists.length > 0){
							self.list = self.list.concat(res.retData.lists);
						}else{
							//加载完成
							self.finished = true;
						}
					}else{
						self.loadError = true;
						Toast(res.retMsg || self.$t('noError'))
					}
				}).catch(error => {
					self.loading = false;
					self.loadError = true;
				})
			},500)
		},
		ctrlShown(){
			//控制弹窗是否显示
			this.shown = !this.shown
		},
		goBack(){
			util.navigateBack(-1,this)
		}
	},
	created() {
		this.current = {
			value:this.$t('backList-key11'),
			index:'1'
		};
		this.defaultDay = this.$t('backList-key11');
		if(this.$route.query.tab) this.pageState = this.$route.query.tab;
		if(this.$route.query.user) this.user = this.$route.query.user;
		this.isSuperPartner = this.$route.query.isSuperPartner;
		util.waitSDKReady().then(token => {
			this.init();
			this.token = token;
		}).catch((e) => {
			Toast(this.$t('getTokenFail'))
		})
		setTimeout(()=>{
			util.registerHandler('refreshCommonRequestWhenGoBackView',res => {
				util.callHandler('setStatusBarStyle',{
					style:'dark'
				})
			})
			//退出登录的时候native调用H5方法清除token缓存
			util.registerHandler('loginOutDeleteToken',res => {
				this.token = '';
				localStorage.removeItem('token');
			})
			util.callHandler('setStatusBarStyle',{
				style:'dark'
			})
		},500)
    }
}